// Package cmd
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

// Posturl the details in config.yaml
type Posturl struct {
	base   string
	suffix string
	method string
	token  string
}

// AliOSS OSS the details in config.yaml
type AliOSS struct {
	accessKeyID     string
	accessKeySecret string
	ossEndpoint     string
	bucketName      string
}

type DingTalk struct {
	dingTalkKey string // var
	dingType    string
	msg         []string
}

type Notify struct {
	Repo    string
	Project string
	Branch  string
	ObjName string
}

type Config struct {
	posturl  Posturl
	oss      AliOSS
	dingTalk DingTalk
	notify   Notify
}

var (
	Conf = new(Config)
	//

)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use:   "deploy [command]",
	Short: "deploy, used to post the file to web and the oss",
	Long: `deploy, 
used to post the file to web and the oss.
	deploy：
		1. post：发布到后台管理端的部署管理模块，
		2. oss: 将结果文件同步到阿里OSS中。
		3. publish: combine the 1 & 2.
		4. notify: with the DingTalk Robot, notify users the message.`,
	// Uncomment the following line if your bare application
	// has an action associated with it:
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(*Conf)
	},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	//
	rootCmd.AddCommand(postCmd)
	//
	rootCmd.AddCommand(ossCmd)
	//
	publish.AddCommand(notify)
	rootCmd.AddCommand(publish)
	//
	err := rootCmd.Execute()
	if err != nil {
		os.Exit(1)
	}
}

func init() {
	//// post url:
	//Conf.posturl.base = "8.129.20.12"
	//Conf.posturl.suffix = "/basis/deployment-file"
	//Conf.posturl.method = "post"
	//// oss:
	//Conf.oss.accessKeyID = "LTAI4GJAtq5WBvytduM5FJr8"
	//Conf.oss.accessKeySecret = "6hzU5i4k9NkdFV4fAGQbWfXU1L3Kbw"
	//Conf.oss.ossEndpoint = "oss-cn-chengdu.aliyuncs.com"
	//Conf.oss.bucketName = "bris4-app-publisher"
	// notify: by the DingTalk robot.
	Conf.dingTalk.dingTalkKey = dingToken
	Conf.dingTalk.dingType = "markdown"
	//Conf.dingTalk.msg = []string{
	//	"drone ->",
	//	"---",
	//	"- Repo: ",
	//	"- Branch: ",
	//	"- Project: ",
	//	"- File: ",
	//	"- Url: ",
	//	"---",
	//	"> ding notify",
	//}

}

func handleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}
